package com.yc.cloud.model.save;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.servlet.http.HttpServletRequest;

import static com.yc.cloud.common.utils.IpUtil.getRealIp;

/**
 * @author 杨智杰
 * @date 2021/8/11 13:12
 */
@Data
@Accessors(chain = true)
public class SaveDataLog {

    @ApiModelProperty(value = "请求方式")
    private String method;

    @ApiModelProperty(value = "请求路径")
    private String requestURI;

    @ApiModelProperty(value = "请求参数")
    private String queryString;

    @ApiModelProperty(value = "请求body")
    private String requestBody;

    @ApiModelProperty(value = "响应body")
    private String responseBody;

    @ApiModelProperty(value = "客户端主机名或IP")
    private String remoteHost;

    @ApiModelProperty(value = "请求协议类型")
    private String scheme;

    @ApiModelProperty(value = "请求协议类型以及版本")
    private String protocol;

    @ApiModelProperty(value = "请求概略信息")
    private String requestURL;

    @ApiModelProperty(value = "请求头信息")
    private String headers;

    @ApiModelProperty("响应时长(毫秒)")
    private Long answer;

    @ApiModelProperty("请求时间")
    private String createTime;

    public SaveDataLog(String method, String requestURI, String queryString, String remoteHost, String scheme, String protocol, String requestURL) {
        this.method = method;
        this.requestURI = requestURI;
        this.queryString = queryString;
        this.remoteHost = remoteHost;
        this.scheme = scheme;
        this.protocol = protocol;
        this.requestURL = requestURL;
    }

    public SaveDataLog(HttpServletRequest request) {
        this(request.getMethod(),
                request.getRequestURI(),
                request.getQueryString(),
                getRealIp(request),
                request.getScheme(),
                request.getProtocol(),
                request.getRequestURL().toString());
    }

    public static SaveDataLog getLogger(HttpServletRequest request) {
        return new SaveDataLog(request);
    }
}
